package com.xbongbong.saas.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.PaasMenuEntity;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.MenuTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.SubFormPoJo;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.model.PaasMenuModel;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.menu.enums.JXCMenuEnum;
import com.xbongbong.pro.script.pojo.dto.ProductDTO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.product.ContractProductEnum;
import com.xbongbong.saas.service.WaitSeriesService;
import com.xbongbong.sys.domain.entity.CoverRoleEntity;
import com.xbongbong.sys.domain.entity.RoleEntity;
import com.xbongbong.sys.model.CompanyModel;
import com.xbongbong.sys.model.CoverRoleModel;
import com.xbongbong.sys.model.RoleModel;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
 * @author jiping.yang
 * @version v1.0
 * @date 21/09/07 10:22
 * @since v1.0
 */
@Service("waitSeriesService")
public class WaitSeriesServiceImpl implements WaitSeriesService {

    private static final Logger LOG = LoggerFactory.getLogger(WaitSeriesServiceImpl.class);

    @Resource
    private CompanyModel companyModel;
    @Resource
    private PaasMenuModel paasMenuModel;
    @Resource
    private RoleModel roleModel;
    @Resource
    private CoverRoleModel coverRoleModel;
    @Resource
    private PaasFormExplainModel paasFormExplainModel;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    @Override
    public String addWaitSeriesMenu(ProductDTO productDTO) throws XbbException {
        LOG.warn("---------------- 开始插入待系列菜单 -------------------");
        long start = DateTimeUtil.getInt();
        // 成功数
        Integer successNum = 0;
        // 如果没传公司ID就取所有公司
        List<String> corpidList = CollectionUtils.isNotEmpty(productDTO.getCorpidList()) ? productDTO.getCorpidList() : companyModel.getJxcCorpidListTemporary();
        int i = 1;
        //更新失败公司ID集合
        Set<String> errorCorpidList = new HashSet<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        //别名集合
        List<String> aliasList = new ArrayList<>();
        aliasList.add(JXCMenuEnum.PURCHASE_INSTOCK.getAlias());
        aliasList.add(JXCMenuEnum.CONTRACT_OUTSTOCK.getAlias());
        aliasList.add(JXCMenuEnum.PURCHASE.getAlias());
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("aliasIn",aliasList);
        params.put("groupByStr","alias");
        params.put("del", DelEnum.NORMAL.getDel());
        for (String corpid : corpidList) {
            int x = corpidList.size() - i;
            LOG.info("-----------------------------当前是第" + i + "家公司,还有" + x + "家是公司需要刷--------------------------");
            i++;
            try {
                params.put("corpid",corpid);
                //根据alias和coprid，查询出表单信息
                List<PaasMenuEntity> paasMenuEntityList= paasMenuModel.list(params);
                List<PaasMenuEntity> paasMenuEntities = new ArrayList<>();
                //将表单信息赋值给对应的待系列
                for (PaasMenuEntity paasMenuEntity : paasMenuEntityList){
                    PaasMenuEntity paasMenu;
                    if (Objects.equals(paasMenuEntity.getAlias() , JXCMenuEnum.PURCHASE_INSTOCK.getAlias())){
                        paasMenu = setDefaultValue(paasMenuEntity);
                        //待入库
                        paasMenu.setName(JXCMenuEnum.WAIT_INSTOCK.getName());
                        paasMenu.setAlias(JXCMenuEnum.WAIT_INSTOCK.getAlias());
                        paasMenu.setUrl(JXCMenuEnum.WAIT_INSTOCK.getAccessLinkEnum().getUrl());
                        paasMenu.setMobileUrl(JXCMenuEnum.WAIT_INSTOCK.getAccessLinkEnum().getMobileUrl());
                        paasMenu.setColor(JXCMenuEnum.WAIT_INSTOCK.getAccessLinkEnum().getColor());
                        paasMenu.setIcon(JXCMenuEnum.WAIT_INSTOCK.getAccessLinkEnum().getIcon());
                        paasMenuEntities.add(paasMenu);
                    }else if (Objects.equals(paasMenuEntity.getAlias() , JXCMenuEnum.CONTRACT_OUTSTOCK.getAlias())){
                        paasMenu = setDefaultValue(paasMenuEntity);
                        //待出库
                        paasMenu.setName(JXCMenuEnum.WAIT_OUTSTOCK.getName());
                        paasMenu.setAlias(JXCMenuEnum.WAIT_OUTSTOCK.getAlias());
                        paasMenu.setUrl(JXCMenuEnum.WAIT_OUTSTOCK.getAccessLinkEnum().getUrl());
                        paasMenu.setMobileUrl(JXCMenuEnum.WAIT_OUTSTOCK.getAccessLinkEnum().getMobileUrl());
                        paasMenu.setColor(JXCMenuEnum.WAIT_OUTSTOCK.getAccessLinkEnum().getColor());
                        paasMenu.setIcon(JXCMenuEnum.WAIT_OUTSTOCK.getAccessLinkEnum().getIcon());
                        paasMenuEntities.add(paasMenu);
                    }else if (Objects.equals(paasMenuEntity.getAlias() , JXCMenuEnum.PURCHASE.getAlias())){
                        paasMenu = setDefaultValue(paasMenuEntity);
                        PaasMenuEntity paasMenuEn = new PaasMenuEntity();
                        BeanUtil.copyProperties(paasMenu , paasMenuEn);
                        //以销定购
                        paasMenu.setName(JXCMenuEnum.WAIT_PURCHASE.getName());
                        paasMenu.setAlias(JXCMenuEnum.WAIT_PURCHASE.getAlias());
                        paasMenu.setUrl(JXCMenuEnum.WAIT_PURCHASE.getAccessLinkEnum().getUrl());
                        paasMenu.setMobileUrl(JXCMenuEnum.WAIT_PURCHASE.getAccessLinkEnum().getMobileUrl());
                        paasMenu.setColor(JXCMenuEnum.WAIT_PURCHASE.getAccessLinkEnum().getColor());
                        paasMenu.setIcon(JXCMenuEnum.WAIT_PURCHASE.getAccessLinkEnum().getIcon());
                        paasMenuEntities.add(paasMenu);
                        //智能补货
                        paasMenuEn.setName(JXCMenuEnum.SMART_REPLENISHMENT.getName());
                        paasMenuEn.setAlias(JXCMenuEnum.SMART_REPLENISHMENT.getAlias());
                        paasMenuEn.setUrl(JXCMenuEnum.SMART_REPLENISHMENT.getAccessLinkEnum().getUrl());
                        paasMenuEn.setMobileUrl(JXCMenuEnum.SMART_REPLENISHMENT.getAccessLinkEnum().getMobileUrl());
                        paasMenuEn.setColor(JXCMenuEnum.SMART_REPLENISHMENT.getAccessLinkEnum().getColor());
                        paasMenuEn.setIcon(JXCMenuEnum.SMART_REPLENISHMENT.getAccessLinkEnum().getIcon());
                        paasMenuEntities.add(paasMenuEn);
                    }
                }
                //批量插入数据
                if (CollectionsUtil.isNotEmpty(paasMenuEntities)){
                    paasMenuModel.insertBatch(paasMenuEntities);
                }
                successNum++;
            } catch (Exception e) {
                LOG.error("公司编号为:" + corpid + "更新失败,失败理由是", e);
                errorCorpidList.add(corpid);
            }
        }
        LOG.error("更新失败公司ID集合" + errorCorpidList.toString());
        LOG.warn("consume:" + (DateTimeUtil.getInt() - start));
        LOG.warn("更新菜单表中数据操作成功,本次共更新了[{}]家公司数据", successNum);
        JSONObject object = new JSONObject();
        object.put("success", "成功");
        return object.toJSONString();
    }

    /**
     * 老用户上线后自动生成待系列菜单
     * @param productDTO 脚本请求类
     * @return
     * @throws XbbException
     */
    public String generatePermissions(ProductDTO productDTO) throws XbbException {
        LOG.warn("---------------- 开始处理待系列老公司权限 -------------------");
        long start = DateTimeUtil.getInt();
        // 成功数
        Integer successNum = 0;
        // 不更新公司数
        Integer notUpdateNum = 0;
        // 如果没传公司ID就取所有公司
        List<String> corpidList = CollectionUtils.isNotEmpty(productDTO.getCorpidList()) ? productDTO.getCorpidList() : companyModel.getCorpidList();
        int i = 1;
        //更新失败公司ID集合
        Set<String> errorCorpidList = new HashSet<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("del",0);
        param.put("columns","id, permissions");
        for (String corpid : corpidList) {
            int x = corpidList.size() - i;
            LOG.info("-----------------------------当前是第" + i + "家公司,还有" + x + "家是公司需要刷--------------------------");
            i++;
            try {
                //获取公司全部角色
                param.put("corpid",corpid);
                //自定义权限
                List<RoleEntity> roleEntityList = roleModel.findAllRoleByCorpid(param);
                //系统权限
                List<CoverRoleEntity> coverRoleEntityList = coverRoleModel.findEntitys(param);
                List<RoleEntity> updateRoleEntity = new ArrayList<>();
                roleEntityList.forEach(roleEnt -> {
                    String permissions = setRolePermissions(roleEnt.getPermissions());
                    if (!Objects.equals(permissions, roleEnt.getPermissions())){
                        RoleEntity roleEntity = new RoleEntity();
                        roleEntity.setId(roleEnt.getId());
                        roleEntity.setPermissions(permissions);
                        updateRoleEntity.add(roleEntity);
                    }
                });
                List<CoverRoleEntity> updateCoverRoleEntity = new ArrayList<>();
                coverRoleEntityList.forEach(coverRoleEnt -> {
                    String permissions = setRolePermissions(coverRoleEnt.getPermissions());
                    if (!Objects.equals(permissions, coverRoleEnt.getPermissions())){
                        CoverRoleEntity coverRoleEntity = new CoverRoleEntity();
                        coverRoleEntity.setId(coverRoleEnt.getId());
                        coverRoleEntity.setPermissions(permissions);
                        updateCoverRoleEntity.add(coverRoleEntity);
                    }
                });
                if (CollectionsUtil.isNotEmpty(updateRoleEntity)){
                    roleModel.updateBatchPermissions(updateRoleEntity, corpid);
                }
                if (CollectionsUtil.isNotEmpty(updateCoverRoleEntity)){
                    coverRoleModel.updateBatchPermissions(updateCoverRoleEntity, corpid);
                }
                if (updateRoleEntity.isEmpty() && updateCoverRoleEntity.isEmpty()){
                    notUpdateNum++;
                }else {
                    successNum++;
                }
            } catch (Exception e) {
                LOG.error("公司编号为:" + corpid + "更新失败,失败理由是", e);
                errorCorpidList.add(corpid);
            }
        }
        LOG.error("更新失败公司ID集合" + errorCorpidList.toString());
        LOG.warn("不更新公司数为:" + notUpdateNum);
        LOG.warn("consume:" + (DateTimeUtil.getInt() - start));
        LOG.warn("更新菜单表中数据操作成功,本次共更新了[{}]家公司数据", successNum);
        JSONObject object = new JSONObject();
        object.put("success", "成功");
        return object.toJSONString();
    }

    /**
     * 老用户上线后自动生成待系列菜单(回滚脚本)
     * @param productDTO 脚本请求类
     * @return
     * @throws XbbException
     */
    public String generatePermissionsRollback(ProductDTO productDTO) throws XbbException {
        LOG.warn("---------------- 开始处理待系列老公司权限 -------------------");
        long start = DateTimeUtil.getInt();
        // 成功数
        Integer successNum = 0;
        // 不更新公司数
        Integer notUpdateNum = 0;
        // 如果没传公司ID就取所有公司
        List<String> corpidList = CollectionUtils.isNotEmpty(productDTO.getCorpidList()) ? productDTO.getCorpidList() : companyModel.getCorpidList();
        int i = 1;
        //更新失败公司ID集合
        Set<String> errorCorpidList = new HashSet<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("del",0);
        param.put("columns","id, permissions");
        for (String corpid : corpidList) {
            int x = corpidList.size() - i;
            LOG.info("-----------------------------当前是第" + i + "家公司,还有" + x + "家是公司需要刷--------------------------");
            i++;
            try {
                //获取公司全部角色
                param.put("corpid",corpid);
                //自定义权限
                List<RoleEntity> roleEntityList = roleModel.findAllRoleByCorpid(param);
                //系统权限
                List<CoverRoleEntity> coverRoleEntityList = coverRoleModel.findEntitys(param);
                List<RoleEntity> updateRoleEntity = new ArrayList<>();
                roleEntityList.forEach(roleEnt -> {
                    String permissions = deleteRolePermissions(roleEnt.getPermissions());
                    if (!Objects.equals(permissions,roleEnt.getPermissions()) && StringUtil.isNotEmpty(permissions)){
                        RoleEntity roleEntity = new RoleEntity();
                        roleEntity.setId(roleEnt.getId());
                        roleEntity.setPermissions(permissions);
                        updateRoleEntity.add(roleEntity);
                    }
                });
                List<CoverRoleEntity> updateCoverRoleEntity = new ArrayList<>();
                coverRoleEntityList.forEach(coverRoleEnt -> {
                    String permissions = deleteRolePermissions(coverRoleEnt.getPermissions());
                    if (!Objects.equals(permissions,coverRoleEnt.getPermissions()) && StringUtil.isNotEmpty(permissions)){
                        CoverRoleEntity coverRoleEntity = new CoverRoleEntity();
                        coverRoleEntity.setId(coverRoleEnt.getId());
                        coverRoleEntity.setPermissions(permissions);
                        updateCoverRoleEntity.add(coverRoleEntity);
                    }
                });
                if (CollectionsUtil.isNotEmpty(updateRoleEntity)){
                    roleModel.updateBatchPermissions(updateRoleEntity, corpid);
                }
                if (CollectionsUtil.isNotEmpty(updateCoverRoleEntity)){
                    coverRoleModel.updateBatchPermissions(updateCoverRoleEntity, corpid);
                }
                if (updateRoleEntity.isEmpty() && updateCoverRoleEntity.isEmpty()){
                    notUpdateNum++;
                }else {
                    successNum++;
                }
            } catch (Exception e) {
                LOG.error("公司编号为:" + corpid + "更新失败,失败理由是", e);
                errorCorpidList.add(corpid);
            }
        }
        LOG.error("更新失败公司ID集合" + errorCorpidList.toString());
        LOG.warn("不更新公司数为:" + notUpdateNum);
        LOG.warn("consume:" + (DateTimeUtil.getInt() - start));
        LOG.warn("更新菜单表中数据操作成功,本次共更新了[{}]家公司数据", successNum);
        JSONObject object = new JSONObject();
        object.put("success", "成功");
        return object.toJSONString();
    }

    /**
     * 合同详情关联产品tap缺货数量改为已采购数量
     * @param productDTO 脚本请求类
     * @return 影响范围
     * @throws XbbException 系统异常
     */
    @Override
    public String purchasedNum(ProductDTO productDTO) throws XbbException{
        LOG.warn("---------------- 开始处理合同子表单解释 -------------------");
        long start = DateTimeUtil.getInt();
        // 成功数
        Integer successNum = 0;
        // 不更新公司数
        Integer notUpdateNum = 0;
        // 如果没传公司ID就取所有公司
        List<String> corpidList = CollectionUtils.isNotEmpty(productDTO.getCorpidList()) ? productDTO.getCorpidList() : companyModel.getCorpidList();
        corpidList.add("0");
        int i = 1;
        //更新失败公司ID集合
        Set<String> errorCorpidList = new HashSet<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("del",0);
        param.put("businessType", XbbRefTypeEnum.CONTRACT.getCode());
        for (String corpid : corpidList) {
            int x = corpidList.size() - i;
            LOG.info("-----------------------------当前是第" + i + "家公司,还有" + x + "家是公司需要刷--------------------------");
            i++;
            param.put("corpid", corpid);
            try{
                List<PaasFormExplainEntity> paasFormExplainEntityList = paasFormExplainModel.findEntitys(param);
                if (CollectionsUtil.isEmpty(paasFormExplainEntityList)) {
                    notUpdateNum ++;
                    continue;
                }
                List<PaasFormExplainEntity> paasFormExplainEntList = new ArrayList<>();
                for (PaasFormExplainEntity paasFormEntityExtList : paasFormExplainEntityList){
                    List<FieldAttrEntity> fieldAttrEntityList = JSONArray.parseArray(paasFormEntityExtList.getExplains(), FieldAttrEntity.class);
                    if (CollectionsUtil.isNotEmpty(fieldAttrEntityList)){
                        for (FieldAttrEntity fieldAttrEntity : fieldAttrEntityList){
                            if (Objects.equals(ContractEnum.PRODUCT.getAttr(), fieldAttrEntity.getAttr())){
                                SubFormPoJo subForm = fieldAttrEntity.getSubForm();
                                if (Objects.isNull(subForm)){
                                    break;
                                }
                                List<FieldAttrEntity> items = (List<FieldAttrEntity>) subForm.getItems();
                                if (CollectionsUtil.isNotEmpty(items)){
                                    for (FieldAttrEntity fieldAttr : items){
                                        if (Objects.equals(ContractProductEnum.OUT_OF_STOCK.getAttr(), fieldAttr.getAttr())){
                                            fieldAttr.setAttrName(ContractProductEnum.OUT_OF_STOCK.getAttrName());
                                            fieldAttr.setAttrNameEn(ContractProductEnum.OUT_OF_STOCK.getAttrName());
                                            fieldAttr.setDefaultName(ContractProductEnum.OUT_OF_STOCK.getAttrName());
                                            fieldAttr.setSaasAttr(ContractProductEnum.OUT_OF_STOCK.getSaasAttr());
                                            break;
                                        }
                                    }
                                }
                                break;
                            }
                        }
                        PaasFormExplainEntity paasFormExplainEntity = new PaasFormExplainEntity();
                        paasFormExplainEntity.setId(paasFormEntityExtList.getId());
                        paasFormExplainEntity.setExplains(JSONObject.toJSONString(fieldAttrEntityList));
                        paasFormExplainEntity.setUpdateTime(DateTimeUtil.getInt());
                        paasFormExplainEntList.add(paasFormExplainEntity);
                    }
                }
                if (CollectionsUtil.isNotEmpty(paasFormExplainEntList)){
                    paasFormExplainModel.updateBatch(paasFormExplainEntList, corpid);
                }
                successNum ++;
            }catch (Exception e){
                LOG.error("公司编号为:" + corpid + "更新失败,失败理由是", e);
                errorCorpidList.add(corpid);
            }
        }
        // 清除公司id为0的合同解释缓存
        paasRedisHelper.removeValue(RedisPrefixConstant.FIELD_EXPLANATION, "0_" + XbbRefTypeEnum.CONTRACT.getCode() + "_0");
        LOG.error("更新失败公司ID集合" + errorCorpidList.toString());
        LOG.warn("不更新公司数为:" + notUpdateNum);
        LOG.warn("consume:" + (DateTimeUtil.getInt() - start));
        LOG.warn("更新菜单表中数据操作成功,本次共更新了[{}]家公司数据", successNum);
        JSONObject object = new JSONObject();
        object.put("success", "成功");
        return object.toJSONString();
    }

    /**
     * 合同详情关联产品tap缺货数量改为已采购数量(回滚脚本)
     * @param productDTO 脚本请求类
     * @return 影响范围
     * @throws XbbException 系统异常
     */
    @Override
    public String purchasedNumRollback(ProductDTO productDTO) throws XbbException{
        LOG.warn("---------------- 开始处理合同子表单解释 -------------------");
        long start = DateTimeUtil.getInt();
        // 成功数
        Integer successNum = 0;
        // 不更新公司数
        Integer notUpdateNum = 0;
        // 如果没传公司ID就取所有公司
        List<String> corpidList = CollectionUtils.isNotEmpty(productDTO.getCorpidList()) ? productDTO.getCorpidList() : companyModel.getCorpidList();
        corpidList.add("0");
        int i = 1;
        //更新失败公司ID集合
        Set<String> errorCorpidList = new HashSet<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("del",0);
        param.put("businessType", XbbRefTypeEnum.CONTRACT.getCode());
        for (String corpid : corpidList) {
            int x = corpidList.size() - i;
            LOG.info("-----------------------------当前是第" + i + "家公司,还有" + x + "家是公司需要刷--------------------------");
            i++;
            param.put("corpid", corpid);
            try{
                List<PaasFormExplainEntity> paasFormExplainEntityList = paasFormExplainModel.findEntitys(param);
                if (CollectionsUtil.isEmpty(paasFormExplainEntityList)) {
                    notUpdateNum ++;
                    continue;
                }
                List<PaasFormExplainEntity> paasFormExplainEntList = new ArrayList<>();
                for (PaasFormExplainEntity paasFormEntityExtList : paasFormExplainEntityList){
                    List<FieldAttrEntity> fieldAttrEntityList = JSONArray.parseArray(paasFormEntityExtList.getExplains(), FieldAttrEntity.class);
                    if (CollectionsUtil.isNotEmpty(fieldAttrEntityList)){
                        for (FieldAttrEntity fieldAttrEntity : fieldAttrEntityList){
                            if (Objects.equals(ContractEnum.PRODUCT.getAttr(), fieldAttrEntity.getAttr())){
                                SubFormPoJo subForm = fieldAttrEntity.getSubForm();
                                if (Objects.isNull(subForm)){
                                    break;
                                }
                                List<FieldAttrEntity> items = (List<FieldAttrEntity>) subForm.getItems();
                                if (CollectionsUtil.isNotEmpty(items)){
                                    for (FieldAttrEntity fieldAttr : items){
                                        if (Objects.equals(ContractProductEnum.OUT_OF_STOCK.getAttr(), fieldAttr.getAttr())){
                                            fieldAttr.setAttrName(SelectProductEnum.OUT_OF_STOCK.getAttrName());
                                            fieldAttr.setAttrNameEn(SelectProductEnum.OUT_OF_STOCK.getAttrName());
                                            fieldAttr.setDefaultName(SelectProductEnum.OUT_OF_STOCK.getAttrName());
                                            fieldAttr.setSaasAttr(SelectProductEnum.OUT_OF_STOCK.getSaasAttr());
                                            break;
                                        }
                                    }
                                }
                                break;
                            }
                        }
                        PaasFormExplainEntity paasFormExplainEntity = new PaasFormExplainEntity();
                        paasFormExplainEntity.setId(paasFormEntityExtList.getId());
                        paasFormExplainEntity.setExplains(JSONObject.toJSONString(fieldAttrEntityList));
                        paasFormExplainEntity.setUpdateTime(DateTimeUtil.getInt());
                        paasFormExplainEntList.add(paasFormExplainEntity);
                    }
                }
                if (CollectionsUtil.isNotEmpty(paasFormExplainEntList)){
                    paasFormExplainModel.updateBatch(paasFormExplainEntList, corpid);
                }
                successNum ++;
            }catch (Exception e){
                LOG.error("公司编号为:" + corpid + "更新失败,失败理由是", e);
                errorCorpidList.add(corpid);
            }
        }
        // 清除公司id为0的合同解释缓存
        paasRedisHelper.removeValue(RedisPrefixConstant.FIELD_EXPLANATION, "0_" + XbbRefTypeEnum.CONTRACT.getCode() + "_0");
        LOG.error("更新失败公司ID集合" + errorCorpidList.toString());
        LOG.warn("不更新公司数为:" + notUpdateNum);
        LOG.warn("consume:" + (DateTimeUtil.getInt() - start));
        LOG.warn("更新菜单表中数据操作成功,本次共更新了[{}]家公司数据", successNum);
        JSONObject object = new JSONObject();
        object.put("success", "成功");
        return object.toJSONString();
    }

    /**
     * 待系列菜单默认值
     * @param paasMenuEntity
     * @return
     */
    public PaasMenuEntity setDefaultValue(PaasMenuEntity paasMenuEntity){
        PaasMenuEntity paasMenu = new PaasMenuEntity();
        paasMenu.setCorpid(paasMenuEntity.getCorpid());
        paasMenu.setAppId(paasMenuEntity.getAppId());
        paasMenu.setParentId(paasMenuEntity.getParentId());
        paasMenu.setSort(BasicConstant.ZERO);
        paasMenu.setType(MenuTypeEnum.MENU.getType());
        paasMenu.setSaasMark(paasMenuEntity.getSaasMark());
        paasMenu.setEnable(paasMenuEntity.getEnable());
        paasMenu.setCreatorId(paasMenuEntity.getCreatorId());
        paasMenu.setAddTime(DateTimeUtil.getInt());
        paasMenu.setUpdateTime(DateTimeUtil.getInt());
        paasMenu.setDel(paasMenuEntity.getDel());
        paasMenu.setDistributorMark(paasMenuEntity.getDistributorMark());
        return paasMenu;
    }

    /**
     * 添加待系列权限
     * @param permissions
     * @return
     */
    public String setRolePermissions(String permissions){
        StringBuilder stringBuilder = new StringBuilder(permissions);
        List<String> permissionsId = Arrays.asList(permissions.split("\\|"));
        if ((permissionsId.contains("304") || permissionsId.contains("707")) && permissionsId.contains("175")){
            permissions = stringBuilder.append("956|957|959|960|").toString();
        }
        if ((permissionsId.contains("304") || permissionsId.contains("707")) && permissionsId.contains("473")){
            permissions = stringBuilder.append("953|954|").toString();
        }
        if (permissionsId.contains("175") && permissionsId.contains("441")){
            permissions = stringBuilder.append("950|951|").toString();
        }
        return permissions;
    }

    /**
     * 删除待系列权限
     * @param permissions
     * @return
     */
    public String deleteRolePermissions(String permissions){
        StringBuilder stringBuilder = new StringBuilder();
        List<String> permissionsId = new ArrayList<>(Arrays.asList(permissions.split("\\|")));
        List<String> deleteId = new ArrayList<>();
        deleteId.add("956");
        deleteId.add("957");
        deleteId.add("959");
        deleteId.add("960");
        deleteId.add("953");
        deleteId.add("954");
        deleteId.add("950");
        deleteId.add("951");
        if (CollectionsUtil.isNotEmpty(permissionsId)){
            permissionsId.removeIf(item -> deleteId.contains(item));
            for (String perId : permissionsId){
                stringBuilder.append(perId).append("|");
            }
        }
        return stringBuilder.toString();
    }
}